from fastapi import APIRouter, Query,UploadFile
from tortoise.expressions import Q

from app.controllers.customer.users import users_controller
from app.controllers.customer.visit import visit_controller
from app.models import User, CustomerUser
from app.schemas.customer import *
from app.schemas.base import Success, SuccessExtra,Fail
from app.core.dependency import DependCustomerAuth
from app.utils.upload import upload_image
from app.core.ctx import CTX_USER_ID

mini_visit_router = APIRouter()
model_name = "会员回访"


@mini_visit_router.get("/list", summary=f"查看{model_name}列表")
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        name: str = Query('', description="客户姓名")
):
    q = Q()
    if name:
        user_ids = await CustomerUser.filter(Q(name__contains=name) | Q(alias__contains=name)).values_list('id', flat=True)
        q &= Q(customer_id___in=list(user_ids))

    total, data = await visit_controller.do_list(page=page, page_size=page_size, search=q)
    for item in data:
        add_user = await User.filter(id=item["add_uid"]).first().values("alias")
        item["add_uname"] = add_user["alias"]
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@mini_visit_router.get("/customer_list", summary=f"查看客户{model_name}列表")
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        customer_id: int = Query(0, description="客户id")
):
    q = Q(customer_id=customer_id)

    total, data = await visit_controller.do_list(page=page, page_size=page_size, search=q)
    for item in data:
        add_user = await User.filter(id=item["add_uid"]).first().values("alias")
        item["add_uname"] = add_user["alias"]
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@mini_visit_router.get("/get", summary=f"查看{model_name}")
async def do_get(
        data_id: int = Query(..., description="ID"),
):
    data = await visit_controller.do_detail(data_id=data_id)
    return Success(data=data)

@mini_visit_router.post("/create", summary=f"创建{model_name}", dependencies=[DependCustomerAuth])
async def do_create(
        data_in:CustomerVisitCreate,
):
    add_uid = CTX_USER_ID.get()
    user_obj = await users_controller.get(id=add_uid)
    data_in.add_uid = user_obj.user_id
    await visit_controller.do_create(obj_in=data_in)
    return Success(msg="创建成功")

@mini_visit_router.post("/update", summary=f"更新{model_name}", dependencies=[DependCustomerAuth])
async def do_update(
        data_in: CustomerVisitUpdate,
):
    add_uid = CTX_USER_ID.get()
    user_obj = await users_controller.get(id=add_uid)
    data_in.add_uid = user_obj.user_id
    await visit_controller.do_update(obj_in=data_in)
    return Success(msg="更新成功")

@mini_visit_router.delete("/delete", summary=f"删除{model_name}")
async def do_delete(
        data_id: int = Query(..., description="ID"),
):
    await visit_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")

@mini_visit_router.post("/file", summary="上传图片")
async def do_file(file: UploadFile):
    url = await upload_image(file,"customers/visit")
    if not url:
        return Fail(code=400, msg="图片格式错误")
    data = {"url": url}
    return Success(data=data)
